import find from 'lodash/find';
import forIn from 'lodash/forIn';
import isEmpty from 'lodash/isEmpty';
import pick from 'lodash/pick';
import {appConfig } from '@/appConfig';

/**
 * 获取 appCodes 对象
 * @param {*} appConfig
 */
export const getAppCodes = (appConfig) => {
    const appCodeObj = {};
    forIn(appConfig, (v, k) => {
        appCodeObj[k] = v.appCode;
    });

    return appCodeObj;
};


/**
 * 根据 key : value ，查找对应的配置；
 * 例如：key='modelName',value='role'，即根据 modelName: 'role' 获取配置
 * @param key
 * @param value
 */
export const getConfigByKeyValue = (key, value) => {
    let configObj = null;
    if (!isEmpty(key) && !isEmpty(value)) {
        for (let k in appConfig) {
            if (appConfig.hasOwnProperty(k)) {
                let tmpO = appConfig[k];
                if (tmpO[key] === value) {
                    configObj = tmpO;
                    break;
                }
            }
        }
    }
    return configObj;
};

/**
 * 根据核算项目资源池标识，查找对应的配置
 * @param resCode
 */
export const getConfigByResCode = (resCode) => {
    return getConfigByKeyValue('resCode', resCode);
};

/**
 * 根据 pathname 获取匹配的 authConfig
 * @param pathname
 */
export const getAuthConfigByPathname = (pathname) => {
    let authConfigObj = null;
    // console.log('pathname', pathname);
    if (!isEmpty(pathname)) {
        for (let k in appConfig) {
            if (appConfig.hasOwnProperty(k)) {
                let tmpO = appConfig[k];

                // 如果有 authConfig.pathAuth，则先匹配这里
                if (tmpO.authConfig && !isEmpty(tmpO.authConfig.pathAuth)) {
                    const pathAuthArr = find(tmpO.authConfig.pathAuth, (val, pathKey) => (pathKey === pathname));
                    if (!isEmpty(pathAuthArr)) {
                        authConfigObj = {
                            ...tmpO.authConfig,
                            actionTypes: { ...pick(tmpO.authConfig.actionTypes, pathAuthArr) },
                        };
                        // console.log(`>>> '${pathname}' pathAuthArr authConfigObj`, pathAuthArr, authConfigObj);
                        break;
                    }
                }

                // 根据 modelName 和 detailRoute 来匹配
                const modelNameInPath = pathname.split('/').filter(v => !isEmpty(v))[0];
                if ((!isEmpty(tmpO.modelName) && tmpO.modelName === modelNameInPath) ||
                    (!isEmpty(tmpO.detailRoute) && tmpO.detailRoute === pathname)) {
                    authConfigObj = tmpO.authConfig;
                }

            }
        }
    }
    return authConfigObj;
};

/**
 * 根据 pathname 获取 appConfig 配置对象
 * @param pathname
 * @returns {null}
 */
export const getConfigByPathname = (pathname) => {
    let configObj = null;

    if (!isEmpty(pathname)) {
        for (let k in appConfig) {
            if (appConfig.hasOwnProperty(k)) {
                let tmpO = appConfig[k];

                // 如果有 authConfig.pathAuth，则先匹配这里
                if (tmpO.authConfig && !isEmpty(tmpO.authConfig.pathAuth)) {
                    const pathAuthArr = find(tmpO.authConfig.pathAuth, (val, pathKey) => (pathKey === pathname));
                    if (!isEmpty(pathAuthArr)) {
                        configObj = tmpO;
                        break;
                    }
                }
                // 根据 modelName 和 detailRoute 来匹配
                const modelNameInPath = pathname.split('/').filter(v => !isEmpty(v))[0];
                if (tmpO.modelName === modelNameInPath || tmpO.detailRoute === pathname) {
                    configObj = tmpO;
                }
            }
        }
    }
    return configObj;
};

/**
 * 判断当前用户是否有权限
 * @param authConfig Object 权限配置
 * @param userAuthResObj Object 用户的权限配置
 * @returns {boolean}
 */
export function isAuthByConfig(authConfig, userAuthResObj) {
    let isAuth = false;
    // console.log('权限。。。。。。',authConfig,userAuthResObj)
    if (!isEmpty(authConfig.actionTypes)) {

        for (let actionKey in authConfig.actionTypes) {
            if (authConfig.actionTypes.hasOwnProperty(actionKey)) {
                let actionVal = authConfig.actionTypes[actionKey];

                let resArr = find(userAuthResObj, (resArr) => (
                    !isEmpty(resArr) && !isEmpty(find(resArr, res => (
                        `${res.moduleCode}${res.functionCode}${res.actionCode}` ===
                        `${authConfig.moduleCode}${authConfig.functionCode}${actionVal}`
                    )))
                ));
                // 有菜单权限
                if (!isEmpty(resArr)) {
                    isAuth = true;
                    // console.log('resArr', resArr);
                    break;
                }
            }
        }
    } else {
        isAuth = true; // 如果该菜单没有配置任何动作权限，则表示菜单是公开的
    }
    return isAuth;
}

export const isAuthByActionType = (actionType, appConfigObj, userAuthResObj) => {
    let hadAuth = true;
    if (actionType && userAuthResObj && appConfigObj) {
        const appAuthConfig = appConfigObj.authConfig || {};
        const actionTypeVal = appAuthConfig.actionTypes ? appAuthConfig.actionTypes[actionType] : '';
        const authConfig = { ...appAuthConfig, actionTypes: {} };
        authConfig.actionTypes[actionType] = actionTypeVal;
        hadAuth = isAuthByConfig(authConfig, userAuthResObj);
    }
    return hadAuth;
};
